﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;

namespace WoChu.RfClient.Duty
{
    public partial class FrmGoodsInfo : Form
    {
        public FrmGoodsInfo()
        {
            InitializeComponent();
            initDataGridSyInfo();
        }

        #region Form事件
        /// <summary>
        /// 扫描枪扫描事件
        /// </summary>
        /// <param name="sender"></p
        private void txtShopNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            //initDataGridSyInfo();
            string goodsCode = this.txtShopNo.Text.ToString().Trim();
            List<LocationGoodsInventoryInfo> list = null;
            List<LocationInventoryInfo> lstLocationInventoryInfo = null;
            if (goodsCode != "")
            {
                try
                {
                    if (goodsCode.Length > 8)//非货位号
                    {
                        initDataGridSyInfoByGoods();
                        if (goodsCode.Substring(0, 1) == "0" && goodsCode.Length == 18)
                        {
                            goodsCode = goodsCode.Substring(0, 10);
                        }
                        else if (goodsCode.Substring(0, 3) == "WOC" && (goodsCode.Length == 17 || goodsCode.Length == 20))
                        {
                            goodsCode = goodsCode.Substring(0, goodsCode.Length - 8);
                        }

                        //list = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoodsByCode(goodsCode).DATA;
                        lstLocationInventoryInfo = Duty.InventoryAdjustmentUIRule.NewInstance.GetGoodsInventoryLstByGoodCode(goodsCode).DATA;
                        if (lstLocationInventoryInfo != null && lstLocationInventoryInfo.Count > 0)
                        {
                            DataSet dsDG = new DataSet();
                            DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
                            for (int i = 0; i < lstLocationInventoryInfo.Count; i++)
                            {
                                DataRow dr = dt.NewRow();
                                dr = dt.NewRow();

                                //List<LocationGoodsInventoryDetail> listCreateDate = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoodsByGoodsId(list[i].GOODSID.ToString()).DATA;

                                //if (listCreateDate != null && listCreateDate.Count > 0)
                                //{
                                //    dr["首次上架"] = listCreateDate[0].CREATETIME.ToString();
                                //    dr["最近上架"] = listCreateDate[listCreateDate.Count - 1].CREATETIME.ToString();
                                //}
                                //else
                                //{
                                //    dr["首次上架"] = "";
                                //    dr["最近上架"] = "";
                                //}
                                dr["货位"] = lstLocationInventoryInfo[i].LOCATIONCODE.ToString();
                                dr["商品编号"] = lstLocationInventoryInfo[i].GOODSCODE.ToString();
                                dr["商品名称"] = lstLocationInventoryInfo[i].GOODSNAME.ToString();

                                dr["批次号"] = lstLocationInventoryInfo[i].GOODSBATCHCODE.ToString();
                                dr["大单位"] = lstLocationInventoryInfo[i].PURUNITNAME.ToString();
                                dr["大单位数量"] = lstLocationInventoryInfo[i].BuyQty.ToString();

                                dr["小单位"] = lstLocationInventoryInfo[i].UNITNAME.ToString();
                                dr["小单位数量"] = lstLocationInventoryInfo[i].StoreQty.ToString();
                                dr["库存数量"] = lstLocationInventoryInfo[i].INVQTY.ToString();
                                dt.Rows.Add(dr);
                            }
                            DataRow drSum = dt.NewRow();
                            drSum["货位"] = "合计";
                            drSum["商品编号"] = lstLocationInventoryInfo[0].GOODSCODE.ToString();
                            drSum["商品名称"] = lstLocationInventoryInfo[0].GOODSNAME.ToString();

                            drSum["批次号"] = "";
                            drSum["大单位"] = lstLocationInventoryInfo[0].PURUNITNAME.ToString();
                            drSum["大单位数量"] = (int)(lstLocationInventoryInfo.Sum(_ => _.INVQTY) / lstLocationInventoryInfo[0].PURUNITQTY);

                            drSum["小单位"] = lstLocationInventoryInfo[0].UNITNAME.ToString();
                            drSum["小单位数量"] = lstLocationInventoryInfo.Sum(_ => _.INVQTY) % lstLocationInventoryInfo[0].PURUNITQTY;
                            drSum["库存数量"] = lstLocationInventoryInfo.Sum(_=>_.INVQTY).ToString();
                            dt.Rows.Add(drSum);

                            dsDG.Tables.Add(dt);
                            this.dataGridSyInfo.DataSource = dsDG.Tables[0];
                        }
                        else
                        {
                            if (goodsCode.Length > 6)
                            {
                                MessageBox.Show("此商品编号暂无数据!");
                            }
                            else
                            {
                                MessageBox.Show("此货位号暂无数据!");
                            }
                        }
                    }
                    else
                    {
                        initDataGridSyInfo();
                        list = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoods(goodsCode).DATA;
                        if (list != null && list.Count > 0)
                        {
                            DataSet dsDG = new DataSet();
                            DataTable dt = (dataGridSyInfo.DataSource as DataTable).Copy();
                            for (int i = 0; i < list.Count; i++)
                            {
                                if (i > 0)
                                {
                                    int numner = 0;
                                    for (int j = 0; j < dt.Rows.Count; j++)
                                    {
                                        if (list[i].INVGOODSCODE.ToString() == dt.Rows[j]["商品编号"].ToString())
                                        {
                                            numner = 1;
                                            dt.Rows[j]["库存数量"] = Convert.ToDouble(dt.Rows[j]["库存数量"].ToString()) + Convert.ToDouble(list[i].QTY.ToString());
                                        }
                                    }
                                    if (numner != 1)
                                    {
                                        DataRow dr = dt.NewRow();
                                        dr = dt.NewRow();

                                        List<LocationGoodsInventoryDetail> listCreateDate = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoodsByGoodsId(list[i].GOODSID.ToString()).DATA;

                                        if (listCreateDate != null && listCreateDate.Count > 0)
                                        {
                                            dr["首次上架"] = listCreateDate[0].CREATETIME.ToString();
                                            dr["最近上架"] = listCreateDate[listCreateDate.Count - 1].CREATETIME.ToString();
                                        }
                                        else
                                        {
                                            dr["首次上架"] = "";
                                            dr["最近上架"] = "";
                                        }
                                        dr["货位"] = list[i].LOCATIONCODE.ToString();
                                        dr["商品编号"] = list[i].INVGOODSCODE.ToString();
                                        dr["商品名称"] = list[i].INVGOODSNAME.ToString();
                                        dr["单位"] = list[i].INVUNITNAME.ToString();
                                        dr["库存数量"] = list[i].QTY.ToString();
                                        dt.Rows.Add(dr);
                                    }

                                }
                                else
                                {
                                    DataRow dr = dt.NewRow();
                                    dr = dt.NewRow();

                                    List<LocationGoodsInventoryDetail> listCreateDate = Duty.LocationGoodsInventoryInfoUIRule.NewInstance.GetLocationGoodsByGoodsId(list[i].GOODSID.ToString()).DATA;

                                    if (listCreateDate != null && listCreateDate.Count > 0)
                                    {
                                        dr["首次上架"] = listCreateDate[0].CREATETIME.ToString();
                                        dr["最近上架"] = listCreateDate[listCreateDate.Count - 1].CREATETIME.ToString();
                                    }
                                    else
                                    {
                                        dr["首次上架"] = "";
                                        dr["最近上架"] = "";
                                    }
                                    dr["货位"] = list[i].LOCATIONCODE.ToString();
                                    dr["商品编号"] = list[i].INVGOODSCODE.ToString();
                                    dr["商品名称"] = list[i].INVGOODSNAME.ToString();

                                    dr["单位"] = list[i].INVUNITNAME.ToString();
                                    dr["库存数量"] = list[i].QTY.ToString();
                                    dt.Rows.Add(dr);
                                }

                            }
                            dsDG.Tables.Add(dt);
                            this.dataGridSyInfo.DataSource = dsDG.Tables[0];
                        }
                        else
                        {
                            if (goodsCode.Length > 6)
                            {
                                MessageBox.Show("此商品编号暂无数据!");
                            }
                            else
                            {
                                MessageBox.Show("此货位号暂无数据!");
                            }
                        }
                    }

                    
                }
                catch (Exception)
                {

                    MessageBox.Show("此货位号暂无数据!");
                }


            }
            else
            {
                MessageBox.Show("请先输入产品编号或者货位号!");
            }
            this.txtShopNo.Text = "";
        }



        #endregion


        /// <summary>
        /// 初始化数据源表头
        /// </summary>
        private void initDataGridSyInfoByGoods()
        {
            DataTable dt = new DataTable("ProfitAndLossInfo");//创建一个名为ProfitAndLossInfo的DataTalbe  

            dt.Columns.Add(new DataColumn("货位", typeof(string)));
            dt.Columns.Add(new DataColumn("商品编号", typeof(string)));//为dt_dry表内建立Column  
            dt.Columns.Add(new DataColumn("商品名称", typeof(string)));
            dt.Columns.Add(new DataColumn("批次号", typeof(string)));
            dt.Columns.Add(new DataColumn("大单位数量", typeof(string)));
            dt.Columns.Add(new DataColumn("大单位", typeof(string)));
            dt.Columns.Add(new DataColumn("小单位数量", typeof(string)));
            dt.Columns.Add(new DataColumn("小单位", typeof(string)));
            dt.Columns.Add(new DataColumn("库存数量", typeof(string)));
            //dt.Columns.Add(new DataColumn("首次上架", typeof(string)));
            //dt.Columns.Add(new DataColumn("最近上架", typeof(string)));

            this.dataGridSyInfo.DataSource = dt;
        }

        private void initDataGridSyInfo()
        {
            DataTable dt = new DataTable("InventoryInfoByLocationCode");//创建一个名为InventoryInfoByLocationCode的DataTalbe  

            dt.Columns.Add(new DataColumn("货位", typeof(string)));
            dt.Columns.Add(new DataColumn("商品编号", typeof(string)));//为dt_dry表内建立Column  
            dt.Columns.Add(new DataColumn("商品名称", typeof(string)));
            dt.Columns.Add(new DataColumn("库存数量", typeof(string)));
            dt.Columns.Add(new DataColumn("单位", typeof(string)));
            dt.Columns.Add(new DataColumn("首次上架", typeof(string)));
            dt.Columns.Add(new DataColumn("最近上架", typeof(string)));

            this.dataGridSyInfo.DataSource = dt;
        }

    }
}